package com.hqyj.shop.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.shop.dto.GoodsInfoDto;
import com.hqyj.shop.pojo.GoodsInfo;
import com.hqyj.shop.mapper.GoodsInfoMapper;
import com.hqyj.shop.service.GoodsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.shop.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2024-10-11 01:33:11
 */
@Service
@CacheConfig(cacheNames = "goodsInfo")
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    @Resource
    GoodsInfoMapper goodsInfoMapper;

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Override
    public ResultVo queryPage(GoodsInfoDto dto) {
        //创建分页查询参数对象
        Page<GoodsInfo> page = new Page<>(dto.getPage(),dto.getRow());
        Page<GoodsInfo>  queryPage =goodsInfoMapper.queryPage(page,dto);
        if(queryPage.getRecords().size() > 0){
            HashMap<String,Object> map = new HashMap<>();
            //当前页集合
            map.put("list",queryPage.getRecords());
            //总条数
            map.put("total",queryPage.getTotal());
            //总页数
            map.put("totalPage",queryPage.getPages());
            return ResultVo.success(map);
        }
        return ResultVo.error("没有查询到数据");
    }



    //如果方法上加上Cacheable 注解，类的其他方法如果直接调用此方法，缓存不会被应用
    @Cacheable(key = "#page+'-'+#row+'-'+#proType+'-'+#brandId+'-'+#startPrice+'-'+#endPrice",value = "goodsInfoPage")
    public Page<GoodsInfo> cache(int page,int row,int proType,int brandId,int startPrice,int endPrice){
        //创建分页查询参数对象
        Page<GoodsInfo> page1 = new Page<>(page,row);
        GoodsInfoDto dto = new GoodsInfoDto();
        dto.setProType(proType);
        dto.setBrandId(brandId);
        dto.setStartPrice(startPrice);
        dto.setEndPrice(endPrice);
        Page<GoodsInfo>  queryPage =goodsInfoMapper.queryPage(page1,dto);
        return queryPage;
    }

    @Override
    @Cacheable(key = "#page+'-'+#row+'-'+#word",value = "goodsInfoFull")
    public Page<GoodsInfo> cacheFull(int page, int row, String word) {
        //创建分页查询参数对象
        Page<GoodsInfo> page1 = new Page<>(page,row);
        Page<GoodsInfo>  queryPage =goodsInfoMapper.queryFullText(page1,word);
        return queryPage;
    }


}
